/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.report_definition_service;

import com.hiddenequity.utils.UrlBuncher;

public class ReportDefinitionService {

	public static final String URL_REPORT_DEFINITION_SERVICE = UrlBuncher.URL_BASE + "ReportDefinitionService";

	private final static String TODAY = "TODAY";
	private final static String YESTERDAY = "YESTERDAY";
	private final static String LAST_7_DAYS = "LAST_7_DAYS";
	private final static String THIS_WEEK_SUN_TODAY = "THIS_WEEK_SUN_TODAY";
	private final static String THIS_WEEK_MON_TODAY = "THIS_WEEK_MON_TODAY";
	private final static String LAST_WEEK = "LAST_WEEK"; 	
	private final static String LAST_14_DAYS = "LAST_14_DAYS";
	private final static String LAST_30_DAYS = "LAST_30_DAYS"; 
	private final static String LAST_BUSINESS_WEEK = "LAST_BUSINESS_WEEK";
	private final static String LAST_WEEK_SUN_SAT = "LAST_WEEK_SUN_SAT"; 
	private final static String THIS_MONTH = "THIS_MONTH";
	private final static String LAST_MONTH = "LAST_MONTH"; 
	private final static String ALL_TIME = "ALL_TIME";
	
	/*
	 * Get date range
	 */
	private static String getDateRange(int dateRangeItem){
		switch (dateRangeItem) {
		case 0:
			return ALL_TIME;
		case 1:
			return TODAY;
		case 2:
			return YESTERDAY;
		case 3:
			return THIS_WEEK_SUN_TODAY;
		case 4:
			return THIS_WEEK_MON_TODAY;
		case 5:
			return LAST_7_DAYS;
		case 6:
			return LAST_WEEK_SUN_SAT;
		case 7:
			return LAST_WEEK; //<====
		case 8:
			return LAST_BUSINESS_WEEK;
		case 9:
			return LAST_14_DAYS;
		case 10:
			return THIS_MONTH;
		case 11:
			return LAST_30_DAYS;
		case 12:
			return LAST_MONTH;
		default:
			return ALL_TIME;
		}
	}//getDateRange

	/*
	 * Define report
	 */
	public static String defineReport(
			String reportType, 
			String[] fields, 
			int dateRangeItem, 
			String[] predicates){
				
		String fieldsTag = "";
		if(fields != null)
			for(String field : fields){
				fieldsTag += "<fields>" + field + "</fields>";
			}
		
		String predicatesTag = "";
		if(predicates != null)
			for(String predicate : predicates){
				predicatesTag += predicate;
			}

//		String dateRangeTag = "";
//		if(!dateRange.get(0).equals("")){
//			dateRangeTag =
//					"<dateRange>" +
//						"<min>" + dateRange.get(0) +"</min>" +
//						"<max>" + dateRange.get(1) +"</max>" +
//					"</dateRange>";
//		}
		
		String reportDefinition = 
			"<reportDefinition>" +
				"<selector>" +
					fieldsTag +
					predicatesTag +
					//dateRangeTag +
					"<ordering>" +
						"<field>Clicks</field>" +
						"<sortOrder>DESCENDING</sortOrder>" +
					"</ordering>" +
//					"<paging>" +
//						"<startIndex>0</startIndex>" +
//						"<numberResults>50</numberResults>" +
//					"</paging>" +
				"</selector>" +
				"<reportName>Custom Performance Report</reportName>" +
				"<reportType>" + reportType + "</reportType>" +
				"<dateRangeType>" + getDateRange(dateRangeItem) + "</dateRangeType>" +
				"<downloadFormat>XML</downloadFormat>" +
			"</reportDefinition>";

		return reportDefinition;
	}//defineReport

	
	
	/*
	 * Get report list
	 */
	public static String setPredicator(String field, String operator, String[] values){
		
		String valuesTag = "";
		for(String value : values){
			valuesTag += "<values>" + value + "</values>";
		}
		
		String predicates =
				"<predicates>" + 
					"<field>" + field + "</field>" + 
					"<operator>" + operator + "</operator>" + 
					valuesTag + 
			    "</predicates>";
		
		return predicates;
	}
	
	
	
//	/*
//	 * Define report
//	 */
//	public static String createReport(){
//		String SOAPBody =
//				"<soap:Body>" +
//					"<mutate xmlns='https://adwords.google.com/api/adwords/cm/v201101' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' >" +
//						"<operations>" +
//							"<operator>ADD</operator>" +
//							"<operand>" +
//							"<selector>" +
//							"<fields>Clicks</fields>" +
//							"<fields>Cost</fields>" +
//							"<fields>Impressions</fields>" +
//							"<fields>TotalConvValue</fields>" +
//							"</selector>" +
//							"<reportName>Account report</reportName>" +
//							"<reportType>ACCOUNT_PERFORMANCE_REPORT</reportType>" +
//							"<dateRangeType>ALL_TIME</dateRangeType>" +
//							"<downloadFormat>XML</downloadFormat>" +
//							"</operand>" +
//						"</operations>" +
//					"</mutate>" +
//				"</soap:Body>";
//		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
//		return envelope;
//	}
	
	
}
